package com.zzyk.main.mapper;


import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface SysUserDao {
    final String SQL_COMMON = "SELECT `id`, `username`, `display_name`, `desc`, `status`, `expire_datetime`, `enterprise`," +
            " `readonly`, `area`, `create_time`, `create_by`, `update_time`, `update_by` FROM tb_sys_user";
    final String SQL_GET_USER_BY_ID = " WHERE `id` = #{userId}";
    final String SQL_GET_USER_BY_NAME = " WHERE `username` = #{username} and isDelete = 0";
    final String SQL_GET_USER_4_AUTH = "SELECT * FROM tb_sys_user WHERE `username` = #{username} and isDelete = 0";
    final String SQL_GET_USER_ISDELETE = " where isDelete = 0 and status !=0 and enterprise = #{enterprise}";

    @Select(SQL_GET_USER_4_AUTH)
    @ResultMap("sysUserWithRoleAndPermission")
    public SysUser getUser4Auth(@Param("username") String username);

    @Select(SQL_COMMON + SQL_GET_USER_BY_ID)
    @ResultMap("sysUser")
    public UserInfo getUserById(@Param("userId") int userId);

    @Select(SQL_COMMON + SQL_GET_USER_BY_NAME)
    @ResultMap("sysUser")
    public UserInfo getUserByUsername(@Param("username") String username);

    @Select(SQL_COMMON + SQL_GET_USER_BY_ID)
    @ResultMap("sysUserWithRoleGroup")
    public UserInfo getUserByIdWithRole(@Param("userId") int userId);

    @Select(SQL_COMMON + SQL_GET_USER_BY_NAME)
    @ResultMap("sysUserWithRole")
    public UserInfo getUserByUsernameWithRole(@Param("username") String username);

    @Select(SQL_COMMON + " WHERE `readonly` = '1' and isDelete = 0")
    @ResultMap("sysUserWithRole")
    public List<UserInfo> getReadonlyUserList();

    @Select(SQL_COMMON + SQL_GET_USER_BY_NAME + " and isDelete = 0")
    @ResultMap("sysUserWithRoleAndPermission")
    public SysUser getUserByUsernameWithRoleAndPermission(@Param("username") String username);

    public List<UserInfo> getUserList(UserInfo userInfo);

    @Select(SQL_COMMON + " WHERE `enterprise`=#{organizationId}" + " and isDelete = 0")
    @ResultMap("sysUserRole")
    public List<UserInfo> getUserByOrganizationId(@Param("organizationId") int organizationId);

    public List<UserInfo> getUserListByArea(UserInfo userInfo);

    public List<UserInfo> getUserListByOrganization(UserInfo userInfo);

    public int addUser(SysUser sysUser);

    public int checkPassword(SysUser sysUser);

    public int updateUser(SysUser sysUser);

    public int deleteUser(@Param("userId") int userId);

    public List<UserInfo> getUserByOrganizationType(@Param("type") int orgType);

    List<UserInfo> selectUserByEnterpise(@Param("id") Integer id);

    int newUser(SysUser sysUser);

    List<SysUser> getAll(@Param("area") String[] area);

    List<UserInfo> getUserListByAreas(@Param("area") String[] area);

    @Select(SQL_COMMON + SQL_GET_USER_BY_NAME)
    @ResultMap("sysUserDeviceList")
    SysUser getUserDetails(@Param("username") String userName);
}
